package mk.company.fin.frame.report.internal;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import mk.company.fin.AppConstants;
import mk.company.fin.frame.internal.AbstractInternalFrame;
import mk.company.fin.reports.model.KontoStatusReport;
import mk.company.fin.service.LocaleService;
import mk.company.fin.service.MessageService;
import mk.company.fin.service.ModelService;

import org.springframework.context.ApplicationContext;

import com.toedter.calendar.JDateChooser;

public class KontoStatusReportFrame extends AbstractInternalFrame {

	private static final long serialVersionUID = 5668158036166496006L;
	
	/* SERVICES */
	
	private MessageService messageService;
	private ModelService modelService;
	private LocaleService localeService;

	
	//component which this frame will be drawn
	JComponent component;
	public KontoStatusReportFrame(JComponent component) {
		this.component=component;
		
	}
	
	@Override
	public void initialize(ApplicationContext context) {
		setContext(context);
		injectServices();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT));
		// components
		initComponents();
	}

	/**
	 * Inject services.
	 */
	private void injectServices() {
		messageService = getContext().getBean(MessageService.class);
		modelService=getContext().getBean(ModelService.class);
		localeService=getContext().getBean(LocaleService.class);
	}
	
	public void initComponents() {
		// title
		setTitle(messageService.getMessage("comintent.conto.plan"));
		// Size
		this.setSize(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT);
		// Maximize
		try {
			this.setMaximum(true);
		} catch (PropertyVetoException e) {
		} catch (NullPointerException n) {
		}
		// Layout
		this.setLayout(null);
		
		initReportSearchPanel();
		initReportTablePanel();
		//Set the layout of all the panels
		setLayout();
		
		this.setVisible(true);
		component.add(this);
	}
	
	private javax.swing.JPanel reportSearchPanel;
	private javax.swing.JLabel fromDateLabel;
    private JDateChooser fromDateField;
    private javax.swing.JLabel toDateLabel;
    private JDateChooser toDateField;
    private javax.swing.JLabel classNumberLabel;
    private javax.swing.JTextField classNumberField;
    private javax.swing.JLabel syntheticNumberLabel;
    private javax.swing.JTextField syntheticNumberField;
    private javax.swing.JButton repotActionButton;
    private javax.swing.JLabel requiredFieldLabel;
    private javax.swing.JLabel requiredFieldLabel2;
	private void initReportSearchPanel(){
		reportSearchPanel=new JPanel();
		reportSearchPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("report")));
		fromDateLabel=new JLabel(messageService.getMessage("date.from"));
		fromDateField = new JDateChooser();
		fromDateField.setLocale(localeService.getCurrentLocale());
		toDateLabel=new JLabel(messageService.getMessage("date.to"));
		toDateField=new JDateChooser();
		toDateField.setLocale(localeService.getCurrentLocale());
		classNumberLabel=new JLabel(messageService.getMessage("conto.class"));
		classNumberField=new JTextField();
		classNumberField.setColumns(1);
		syntheticNumberLabel=new JLabel(messageService.getMessage("conto.synthetic"));
		syntheticNumberField=new JTextField();
		syntheticNumberField.setColumns(3);
		repotActionButton=new JButton();
		repotActionButton.setText(messageService.getMessage("report"));
		requiredFieldLabel=new JLabel();
		requiredFieldLabel.setForeground(new java.awt.Color(255, 0, 0));
        requiredFieldLabel.setText("*");
        requiredFieldLabel2=new JLabel();
        requiredFieldLabel2.setForeground(new java.awt.Color(255, 0, 0));
        requiredFieldLabel2.setText("*");
        repotActionButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (fromDateField.getDate()==null){
					JOptionPane.showMessageDialog(component, messageService.getMessage("report.no.start.date"),"", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if (toDateField.getDate()==null){
					JOptionPane.showMessageDialog(component, messageService.getMessage("report.no.end.date"),"", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if (!syntheticNumberField.getText().equals("")&&syntheticNumberField.getText().length()!=3){
					return;
				}
				((DefaultTableModel)reportTable.getModel()).getDataVector().removeAllElements();
				List<KontoStatusReport> resultList=modelService.getKontoStatusReport(fromDateField.getDate(), toDateField.getDate(), classNumberField.getText(),syntheticNumberField.getText());
				for (KontoStatusReport kontoStatusReport:resultList){
					Vector<String> row=new Vector<String>();
					row.add(kontoStatusReport.getKonto());
					row.add(kontoStatusReport.getKomintentCode());
					row.add(kontoStatusReport.getKomintentName());
					row.add(kontoStatusReport.getMTR());
					row.add(kontoStatusReport.getStartDebt());
					row.add(kontoStatusReport.getStartDemand());
					row.add(kontoStatusReport.getCurrentDebt());
					row.add(kontoStatusReport.getCurrentDemand());
					try {
						row.add((Double.parseDouble(kontoStatusReport.getStartDebt())-Double.parseDouble(kontoStatusReport.getStartDemand())+Double.parseDouble(kontoStatusReport.getCurrentDebt())-Double.parseDouble(kontoStatusReport.getCurrentDemand()))+"");
					}
					catch (Exception ex){}
					((DefaultTableModel) reportTable.getModel()).addRow(row);
				}
				reportTable.revalidate();
			}
		});
	}
	
    private javax.swing.JPanel reportPanel;
    private javax.swing.JScrollPane reportScrollPane;
   
    private javax.swing.JTable reportTable;
	@SuppressWarnings("all")
	private void initReportTablePanel(){
		reportPanel=new JPanel();
		reportPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("report")));
		reportScrollPane = new javax.swing.JScrollPane();
		reportTable = new javax.swing.JTable();
		reportTable.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [][] {
	            },
	            new String [] {
	                "Конто", "Шифра на коминтент", "Име на коминтент", "МТР", "Почетно должи", "Почетно побарува", "Моментално должи", "Моментално побарува", "Салдо"
	            }
	        ) {
	            Class[] types = new Class [] {
	                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
	            };
	            boolean[] canEdit = new boolean [] {
	                false, false, false, false, false, false, false, false, false
	            };

	            public Class getColumnClass(int columnIndex) {
	                return types [columnIndex];
	            }

	            public boolean isCellEditable(int rowIndex, int columnIndex) {
	                return canEdit [columnIndex];
	            }
	        });
	        reportScrollPane.setViewportView(reportTable);
	        reportTable.setAutoCreateRowSorter(true);
	}
	
	
	@Override
	public void draw() {
		injectServices();
		initComponents();
	}
	
	private void setLayout(){
		 javax.swing.GroupLayout reportSearchPanelLayout = new javax.swing.GroupLayout(reportSearchPanel);
	        reportSearchPanel.setLayout(reportSearchPanelLayout);
	        reportSearchPanelLayout.setHorizontalGroup(
	            reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(reportSearchPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                    .addGroup(reportSearchPanelLayout.createSequentialGroup()
	                        .addComponent(fromDateLabel)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                        .addComponent(fromDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(requiredFieldLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(18, 18, 18)
	                        .addComponent(toDateLabel))
	                    .addGroup(reportSearchPanelLayout.createSequentialGroup()
	                        .addComponent(classNumberLabel)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                        .addComponent(classNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	                        .addComponent(syntheticNumberLabel)))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(reportSearchPanelLayout.createSequentialGroup()
	                        .addComponent(syntheticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(18, 18, 18)
	                        .addComponent(repotActionButton))
	                    .addGroup(reportSearchPanelLayout.createSequentialGroup()
	                        .addComponent(toDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(requiredFieldLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)))
	                .addContainerGap(20, Short.MAX_VALUE))
	        );
	        reportSearchPanelLayout.setVerticalGroup(
	            reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(reportSearchPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(fromDateLabel)
	                    .addComponent(fromDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(toDateLabel)
	                    .addComponent(toDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(requiredFieldLabel)
	                    .addComponent(requiredFieldLabel2))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
	                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(classNumberLabel)
	                    .addComponent(classNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(syntheticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                    .addComponent(syntheticNumberLabel)
	                    .addComponent(repotActionButton))
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
	        javax.swing.GroupLayout reportPanelLayout = new javax.swing.GroupLayout(reportPanel);
	        reportPanel.setLayout(reportPanelLayout);
	        reportPanelLayout.setHorizontalGroup(
	            reportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(reportPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(reportScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 554, Short.MAX_VALUE)
	                .addContainerGap())
	        );
	        reportPanelLayout.setVerticalGroup(
	            reportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(reportPanelLayout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(reportScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );

	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addGap(22, 22, 22)
	                        .addComponent(reportSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                        .addGap(0, 0, Short.MAX_VALUE))
	                    .addGroup(layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addComponent(reportPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
	                .addContainerGap())
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addComponent(reportSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addGap(18, 18, 18)
	                .addComponent(reportPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
	                .addContainerGap(28, Short.MAX_VALUE))
	        );
	}

}
